package com.softjourn.punchcard.merchant.activity;

import java.util.concurrent.ExecutionException;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockActivity;
import com.softjourn.punchcard.merchant.R;
import com.softjourn.punchcard.merchant.lib.ApiFunctions;
import com.softjourn.punchcard.merchant.lib.NetworkUtil;
import com.softjourn.punchcard.merchant.lib.PropertiesStorage;
import com.softjourn.punchcard.merchant.model.ApiResponse;
import com.softjourn.punchcard.merchant.model.Offer;
import com.softjourn.punchcard.merchant.model.OfferResponse;

public class OfferActivity extends SherlockActivity {

	private PropertiesStorage propertiesStorage;

	Button btnCancel;
	Button btnDebitBonus;

	TextView offerName;
	TextView offerMarkCount;
	
	Dialog dialog;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		propertiesStorage = new PropertiesStorage(this);

		setContentView(R.layout.activity_offer);

		btnCancel = (Button) findViewById(R.id.btn_offer_cancel);
		btnDebitBonus = (Button) findViewById(R.id.btn_debit_bonus);

		offerName = (TextView) findViewById(R.id.offer_name);
		offerMarkCount = (TextView) findViewById(R.id.offer_mark_count);
		
		// cancel button should take us to activity_main layout
		btnCancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		if (NetworkUtil.isConnectivityActive(getApplicationContext())) {

			try {
				
				final String offerUid = propertiesStorage.getActiveOffer();
				
				final String punchCardUid = propertiesStorage.getActivePunchCard();
				
				OfferResponse offerResponse = new GetOfferAsyncTask()
						.execute(offerUid).get();
				
				if (offerResponse.getStatus().equals("OK")) {

					final Offer offer = offerResponse.getOffer();
					
					if (offer != null) {
						
						offerName.setText(offer.getName());
						offerMarkCount.setText(Integer.toString(offer.getBonuses()));
						
						btnDebitBonus.setOnClickListener(new OnClickListener() {
							@Override
							public void onClick(View v) {
								try {
									
									ApiResponse apiResponse = new DebitBonusesAsyncTask()
											.execute(punchCardUid, offerUid).get();
									
									if (apiResponse.getStatus().equals("OK")) {
										String message = getString(R.string.debit_success);
										
										message = message.replace(getString(R.string.mask_mark_count),
												Integer.toString(offer.getBonuses()));
										
										showMessageDialog(message);
									} else {
										showMessageDialog(getString(R.string.debit_error)
												+ apiResponse.getStatus());
									}
			
								} catch (InterruptedException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								} catch (ExecutionException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
			
							}
						});
						
					} else {
						// server error mess
					}
					
				} else {
					// server error mess
				}

			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} else {

		}
	}
	
	private void showMessageDialog(String message) {
		
		dialog = new Dialog(this,R.style.Theme_Punchcard_PopUpDialog);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.dialog_message);
		
		ColorDrawable dialogColor = new ColorDrawable(Color.WHITE);
		dialogColor.setAlpha(220);
		dialog.getWindow().setBackgroundDrawable(dialogColor);
		
		TextView dialogMessage = (TextView) dialog.findViewById(R.id.dialogMessage);
		Button btnReturn = (Button) dialog.findViewById(R.id.btnReturn);
		
		dialogMessage.setText(message);
		
		btnReturn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.cancel();
			}
		});
		
		dialog.setCanceledOnTouchOutside(true);
		
		dialog.setOnCancelListener(new OnCancelListener() {
			@Override
			public void onCancel(DialogInterface dialog) {
				finish();
			}
		});
		
		dialog.show();
	}

	private class GetOfferAsyncTask extends
			AsyncTask<String, Void, OfferResponse> {

		@Override
		protected OfferResponse doInBackground(String... params) {

			ApiFunctions merchantFunction = new ApiFunctions(
					OfferActivity.this);
			try {
				OfferResponse response = merchantFunction
						.getOffer(params[0]);
				Log.i("OK", response.toString());
				return response;

			} catch (Exception e) {
				Log.e("EXCEPTION", e.getMessage());
			}

			return null;
		}
	}

	private class DebitBonusesAsyncTask extends
			AsyncTask<String, Void, ApiResponse> {

		@Override
		protected ApiResponse doInBackground(String... params) {

			ApiFunctions merchantFunction = new ApiFunctions(
					OfferActivity.this);
			try {
				ApiResponse response = merchantFunction.debitBonuses(params[0],
						params[1]);
				Log.i("OK", response.toString());
				return response;

			} catch (Exception e) {
				Log.e("EXCEPTION", e.getMessage());
			}

			return null;
		}
	}
	
}
